<?php include("conn.php"); ?>
<?php

//some function
function shutdown(){
	system("sudo shutdown -h now", $res);
	return "1";
}

function resetting(){
	$dir = get_dir();
	mysql_query("delete from register")
		or die(mysql_error());
	//mysql_query("update register set state = 2
	//	where dir = '$dir'") or die(mysql_error());
	return "1";
}

function get_path(){
	$res = mysql_query("select * from setting where name = 'path'")
		or die(mysql_error());
	$res = mysql_fetch_array($res);

	return $res['value'];
}

function get_dir(){
	$res = mysql_query("select * from log order by id desc") or die(mysql_error());
	$res = mysql_fetch_array($res);
	return $res["dir"];
}

function register($serial, $dir){
	$res = mysql_query("select * from register where serial = '$serial'") or die(mysql_error());

	if(mysql_num_rows($res) == 0){
		mysql_query("insert into register (serial, dir, state)
			VALUES ('$serial', '$dir', 0)") or die(mysql_error());
		return "1";
	}

	$res = mysql_fetch_array($res);
	$dir = get_dir();

	if($res["dir"] != $dir){
		mysql_query("update register set state = 0,dir = '$dir'
			where serial = '$serial'") or die(mysql_error());
		return "1";
	}else{
		if($res['state'] == 2)
			return "0";
		else
			return "1";
	}
}

function login($serial){
	$res = mysql_query("update register set state = 1
		where serial = '$serial'") or die(mysql_error());
	return "1";
}

function logout($serial){
	mysql_query("update register set state = 2
			where serial = '$serial'") or die(mysql_error());
	return "1";
}

function check_state(){
	$dir = get_dir();
	$res = mysql_query("select * from register order by id") or die(mysql_error());

	$data = "";

	while($row = mysql_fetch_array($res)){
		if($dir == $row['dir']){
			if($row['state'] == 2)
				$state = "传输完成";
			else if($row['state'] == 1)
				$state = "正在传输";
			else
				$state = "等待传输";
		}else{
			$state = "设备不在线或等待注册";
			$id = $row['id'];
			//mysql_query("delete from register where id = $id") or die(mysql_error());
		}
		$data = $data."<tr><td>".$row['serial']."</td><td>".$state."</td></tr>";
	}
	return $data;
}

function query_state($serial){

	$res = mysql_query("select * from register where serial = '$serial'") or die(mysql_error());

	if(mysql_numrows($res) == 0){
		return "1";
	}else{
		mysql_data_seek($res, 1);
		$row = mysql_fetch_assoc($res);

		if($row['state'] == 2){
			return "0";
		}else{
			return "1";
		}
	}

}

function set_start(){
	$old_dir = get_dir();
	$res = mysql_query("select * from register where dir = '$old_dir'") or die(mysql_error());
	$count1 = mysql_num_rows($res);

	$res = mysql_query("select * from register where dir = '$old_dir' and state = 2") or die(mysql_error());
	$count2 = mysql_num_rows($res);


	if($count1 != $count2){
		return "0";
	}else{
		$new_dir = date("Y-m-d H:i:s");

		$res = mysql_query("select * from setting where name = 'path'")
			or die(mysql_error());
		$res = mysql_fetch_array($res);

		mkdir($res['value'].$new_dir, 0777);
		chmod($res['value'].$new_dir, 0777);

		mysql_query("insert into log (dir)
			VALUES ('$new_dir')") or die(mysql_error());

		mysql_query("delete from register")
			or die(mysql_error());
		#mysql_query("update register set state = 0,dir = 'default'
		#	where dir = '$old_dir' and state = 2") or die(mysql_error());
		return "1";
	}
}
?>
<?php
if(isset($_GET['act'])){
	$act = $_GET['act'];

	if($act == 'get_dir'){
		die(get_dir());
	}else if($act == 'get_path'){
		die(get_path());
	}else if($act == 'check_state'){
		die(check_state());
	}else if($act == 'set_start'){
		die(set_start());
	}else if($act == 'reset'){
		die(resetting());
	}else if($act == 'shutdown'){
		die(shutdown());
	}else{
		if(isset($_GET['serial'])){
			$serial = $_GET['serial'];
			if($act == "login"){
				die(login($serial));
			}else if($act == "logout"){
				die(logout($serial));
			}else if($act == "query_state"){
				die(query_state($serial));
			}
			if(isset($_GET['dir'])){
				$dir = $_GET['dir'];
				if($act == "register"){
					die(register($serial, $dir));
				}
			}
		}
	}
}
?>
<!DOCTYPE HTML>
<html lang="zh-CN">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
	<meta charset="UTF-8">
	<head>
		<title>照片管理中心</title>
		<link rel="stylesheet" href="css/bootstrap.min.css">
		<script src="js/jquery.min.js"></script>
		<script src="js/bootstrap.min.js"></script>
	</head>
	<body>
		<div class="panel panel-primary">
			<div class="panel-heading">
				<h3 class="panel-title">照片管理中心</h3>
			</div>
			<div class="panel-body">
				<button type="button" class="btn btn-default" id="send">上传</button>
				<button type="button" class="btn btn-warning" id="reset">重置</button>
				<button type="button" class="btn btn-danger" id="shutdown">关机</button>
			</div>

			<table class="table">
				<thead>
					<tr>
						<th>
							设备序列号
						</th>
						<th>
							状态
						</th>
					</tr>
				</thead>
				<tbody id="container">
				</tbody>
			</table>
		</div>
	</body>
	<script>
		$(document).ready(function(){

			$("#send").click(function(){
				$.get("index.php?act=set_start", function(res){
					if(res == 1){
						alert("操作成功");
					}else{
						alert("当前还有设备正在工作");
					}
				});
			});

			$("#reset").click(function(){
				if(confirm("确定重置当前任务？")){
					$.get("index.php?act=reset", function(res){
						if(res == 1){
							alert("操作成功");
						}else{
							alert("发生未知错误");
						}
					});

				}
			});

			$("#shutdown").click(function(){
				if(confirm("确定关闭文件服务器？")){
					$.get("index.php?act=shutdown", function(res){
						if(res == 1){
							alert("操作成功");
						}else{
							alert("发生未知错误");
						}
					});

				}

			});

			setInterval(function(){
				$.get("index.php?act=check_state", function(res){
					//alert(res);
					var tr = $(res);
					var tbody = $("#container");
					$("tbody").empty();
					tr.appendTo(tbody);
				})
			}, 1000);
		});
	</script>
</html>
